﻿using MapleLeaf.Pay.Pay;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Management;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Markup;

namespace MapleLeaf.Client.Pages {

    public partial class ConfigPage : Page, IComponentConnector, IStyleConnector {

        public ConfigPage() : base() {
            InitializeComponent();
        }
        public static List<string> PrinterPorts {
            get {
                List<string> list = SerialPort.GetPortNames().ToList();
                list.InsertRange(0, new string[]
                {
                    "LPT1",
                    "LPT2",
                    "LPT3"
                });
                return list;
            }
        }

        public static List<string> PrinterNames {
            get {
                List<string> printerPorts = PrinterPorts;
                try {
                    ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("SELECT PNPDeviceID From Win32_PnPEntity where service = 'usbprint'");
                    foreach (ManagementBaseObject managementBaseObject in managementObjectSearcher.Get()) {
                        string text = managementBaseObject["PNPDeviceID"] as string;
                        printerPorts.Add(string.Format("\\\\?\\{0}#{{28d78fad-5a12-11d1-ae5b-0000f803a8c2}}", text.Replace('\\', '#')).ToLower());
                    }
                }
                catch {
                }
                return printerPorts;
            }
        }

        private void Save_Click(object sender, RoutedEventArgs e) {
            HongshPayClientContext.Default.Config.Save();
            foreach (HongshPayClientPlugin hongshPayClientPlugin in HongshPayClientContext.Default.Plugins) {
                if (hongshPayClientPlugin.Config != null) {
                    hongshPayClientPlugin.Config.Save();
                }
            }
            _ = MessageBox.Show("配置文件保存成功！", App.Title, MessageBoxButton.OK, MessageBoxImage.Asterisk);
        }


        private void FileBrowser_Click(object sender, RoutedEventArgs e) {
        }


        private void PluginCommand(object sender, RoutedEventArgs e) {
            HongshPayClientPlugin hongshPayClientPlugin = (sender as Button).CommandParameter as HongshPayClientPlugin;
            if (hongshPayClientPlugin.Config.IsRunning) {
                hongshPayClientPlugin.Stop();
            }
            else {
                hongshPayClientPlugin.Start();
            }
        }


        private void HotkeyButton_LostFocus(object sender, RoutedEventArgs e) {
            bool? isChecked = (sender as ToggleButton).IsChecked;
            if (isChecked.GetValueOrDefault() & isChecked != null) {
                (sender as ToggleButton).IsChecked = new bool?(false);
            }
        }

        private void HKShowHideButton_PreviewKeyDown(object sender, KeyEventArgs e) {
            bool? isChecked = (sender as ToggleButton).IsChecked;
            if (isChecked.GetValueOrDefault() & isChecked != null) {
                (DataContext as HongshPayClientContext).Config.HKShowHide = e.Key.ToString();
                (sender as ToggleButton).IsChecked = new bool?(false);
            }
        }


        public void Connect(int connectionId, object target) {
            if (connectionId == 2) {
                ((Button)target).Click += this.PluginCommand;
            }
        }
    }
}
